home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / src / uuencode.c < prev   
Encoding:
C/C++ Source or Header  |  1995-11-24  |  7.8 KB  |  300 lines

  1. /* uuencode utility.
  2.    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    This product is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This product is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this product; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  17.    02111-1307, USA.  */
  18.  
  19. /* Copyright (c) 1983 Regents of the University of California.
  20.    All rights reserved.
  21.  
  22.    Redistribution and use in source and binary forms, with or without
  23.    modification, are permitted provided that the following conditions
  24.    are met:
  25.    1. Redistributions of source code must retain the above copyright
  26.       notice, this list of conditions and the following disclaimer.
  27.    2. Redistributions in binary form must reproduce the above copyright
  28.       notice, this list of conditions and the following disclaimer in the
  29.       documentation and/or other materials provided with the distribution.
  30.    3. All advertising materials mentioning features or use of this software
  31.       must display the following acknowledgement:
  32.      This product includes software developed by the University of
  33.      California, Berkeley and its contributors.
  34.    4. Neither the name of the University nor the names of its contributors
  35.       may be used to endorse or promote products derived from this software
  36.       without specific prior written permission.
  37.  
  38.    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  39.    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40.    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41.    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  42.    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43.    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44.    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45.    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46.    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47.    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48.    SUCH DAMAGE.  */
  49.  
  50. /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
  51.  
  52. #include "system.h"
  53.  
  54. /*=======================================================\
  55. | uuencode [INPUT] OUTPUT                 |
  56. |                              |
  57. | Encode a file so it can be mailed to a remote system.     |
  58. \=======================================================*/
  59.  
  60. #include "getopt.h"
  61.  
  62. #define    RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
  63.  
  64. static struct option longopts[] =
  65. {
  66.   { "base64", 0, 0, 'm' },
  67.   { "version", 0, 0, 'v' },
  68.   { "help", 0, 0, 'h' },
  69.   { NULL, 0, 0, 0 }
  70. };
  71.  
  72. static void encode __P ((void));
  73. static void usage __P ((int));
  74.  
  75. /* The name this program was run with. */
  76. const char *program_name;
  77.  
  78. /* Pointer to the translation table we currently use.  */
  79. const char *trans_ptr;
  80.  
  81. /* The two currently defined translation tables.  The first is the
  82.    standard uuencoding, the second is base64 encoding.  */
  83. const char uu_std[64] =
  84. {
  85.   '`', '!', '"', '#', '$', '%', '&', '\'',
  86.   '(', ')', '*', '+', ',', '-', '.', '/',
  87.   '0', '1', '2', '3', '4', '5', '6', '7',
  88.   '8', '9', ':', ';', '<', '=', '>', '?',
  89.   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  90.   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  91.   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  92.   'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
  93. };
  94.  
  95. const char uu_base64[64] =
  96. {
  97.   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  98.   'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  99.   'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  100.   'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  101.   'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  102.   'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  103.   'w', 'x', 'y', 'z', '0', '1', '2', '3',
  104.   '4', '5', '6', '7', '8', '9', '+', '/'
  105. };
  106.  
  107. /* ENC is the basic 1 character encoding function to make a char printing.  */
  108. #define ENC(Char) (trans_ptr[(Char) & 077])
  109.  
  110. /*------------------------------------------------.
  111. | Copy from IN to OUT, encoding as you go along.  |
  112. `------------------------------------------------*/
  113.  
  114. static void
  115. encode ()
  116. {
  117.   register int ch, n;
  118.   register char *p;
  119.   char buf[80];
  120.  
  121.   while (1)
  122.     {
  123.       n = 0;
  124.       do
  125.     {
  126.       register int m = fread (buf, 1, 45 - n, stdin);
  127.       if (m == 0)
  128.         break;
  129.       n += m;
  130.     }
  131.       while (n < 45);
  132.  
  133.       if (n == 0)
  134.     break;
  135.  
  136.       if (trans_ptr == uu_std)
  137.     if (putchar (ENC (n)) == EOF)
  138.       break;
  139.       for (p = buf; n > 2; n -= 3, p += 3)
  140.     {
  141.       ch = *p >> 2;
  142.       ch = ENC (ch);
  143.       if (putchar (ch) == EOF)
  144.         break;
  145.       ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
  146.       ch = ENC (ch);
  147.       if (putchar (ch) == EOF)
  148.         break;
  149.       ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  150.       ch = ENC (ch);
  151.       if (putchar (ch) == EOF)
  152.         break;
  153.       ch = p[2] & 077;
  154.       ch = ENC (ch);
  155.       if (putchar (ch) == EOF)
  156.         break;
  157.     }
  158.  
  159.       if (n != 0)
  160.     break;
  161.  
  162.       if (putchar ('\n') == EOF)
  163.     break;
  164.     }
  165.  
  166.   while (n != 0)
  167.     {
  168.       char c1 = *p;
  169.       char c2 = n == 1 ? 0 : p[1];
  170.  
  171.       ch = c1 >> 2;
  172.       ch = ENC (ch);
  173.       if (putchar (ch) == EOF)
  174.     break;
  175.  
  176.       ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017);
  177.       ch = ENC (ch);
  178.       if (putchar (ch) == EOF)
  179.     break;
  180.  
  181.       if (n == 1)
  182.     ch = trans_ptr == uu_std ? ENC ('\0') : '=';
  183.       else
  184.     {
  185.       ch = (c2 << 2) & 074;
  186.       ch = ENC (ch);
  187.     }
  188.       if (putchar (ch) == EOF)
  189.     break;
  190.       ch = trans_ptr == uu_std ? ENC ('\0') : '=';
  191.       if (putchar (ch) == EOF)
  192.     break;
  193.       putchar ('\n');
  194.       break;
  195.     }
  196.  
  197.   if (ferror (stdin))
  198.     error (EXIT_FAILURE, 0, _("Read error"));
  199.   if (trans_ptr == uu_std)
  200.     {
  201.       putchar (ENC ('\0'));
  202.       putchar ('\n');
  203.     }
  204. }
  205.  
  206. static void
  207. usage (status)
  208.      int status;
  209. {
  210.   if (status != 0)
  211.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  212.          program_name);
  213.   else
  214.     {
  215.       printf (_("Usage: %s [INFILE] REMOTEFILE\n"), program_name);
  216.       printf (_("\n\
  217.   -h, --help      display this help and exit\n\
  218.   -m, --base64    use base64 encoding as of RFC1521\n\
  219.   -v, --version   output version information and exit\n"));
  220.     }
  221.   exit (status);
  222. }
  223.  
  224. int
  225. main (argc, argv)
  226.      int argc;
  227.      char *const *argv;
  228. {
  229.   int opt;
  230.   struct stat sb;
  231.   int mode;
  232.  
  233.   /* Set global variables.  */
  234.   trans_ptr = uu_std;        /* Standard encoding is old uu format.  */
  235.  
  236.   program_name = argv[0];
  237.   setlocale (LC_ALL, "");
  238.  
  239.   /* Set the text message domain.  */
  240.   bindtextdomain (PACKAGE, LOCALEDIR);
  241.   textdomain (PACKAGE);
  242.  
  243.   while (opt = getopt_long (argc, argv, "hmv", longopts, (int *) NULL),
  244.      opt != EOF)
  245.     {
  246.       switch (opt)
  247.     {
  248.     case 'h':
  249.       usage (EXIT_SUCCESS);
  250.  
  251.     case 'm':
  252.       trans_ptr = uu_base64;
  253.       break;
  254.  
  255.     case 'v':
  256.       printf ("%s - GNU %s %s\n", program_name, PACKAGE, VERSION);
  257.       exit (EXIT_SUCCESS);
  258.  
  259.     case 0:
  260.       break;
  261.  
  262.     default:
  263.       usage (EXIT_FAILURE);
  264.     }
  265.     }
  266.  
  267.   switch (argc - optind)
  268.     {
  269.     case 2:
  270.  
  271.       /* Optional first argument is input file.  */
  272.  
  273.       if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb))
  274.     error (EXIT_FAILURE, errno, "%s", argv[optind]);
  275.       mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
  276.       optind++;
  277.       break;
  278.  
  279.     case 1:
  280.       mode = RW & ~umask (RW);
  281.       break;
  282.  
  283.     case 0:
  284.     default:
  285.       usage (EXIT_FAILURE);
  286.     }
  287.  
  288. #if S_IRWXU != 0700
  289. choke me - Must translate mode argument
  290. #endif
  291.  
  292.   printf ("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
  293.       mode, argv[optind]);
  294.   encode ();
  295.   printf (trans_ptr == uu_std ? "end\n" : "====\n");
  296.   if (ferror (stdout))
  297.     error (EXIT_FAILURE, 0, _("Write error"));
  298.   exit (EXIT_SUCCESS);
  299. }
  300.